﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.GameObjects;
using Microsoft.Xna.Framework;
using FarseerGames.FarseerPhysics.Factories;
using LaughingDog.Managers;

namespace LaughingDog.Components
{
    public class LinearSpring : Spring
    {
        private FarseerGames.FarseerPhysics.Dynamics.Springs.LinearSpring mLinearSpring = null;

        private RigidBody mConnectedBody;

        /// <summary>
        /// The second RigidBody this Spring is attached to. The first RigidBody is attached to the GameObject that the Spring is attached to.
        /// </summary>
        public RigidBody ConnectedBody { get { return mConnectedBody; } set { mConnectedBody = value; } }

        /// <summary>
        /// Gets or sets the local anchor for the first RigidBody.
        /// </summary>
        public Vector2 Anchor1 { get { return mLinearSpring.AttachPoint1; } set { mLinearSpring.AttachPoint1 = value; } }

        /// <summary>
        /// Gets or sets the local anchor for the second RigidBody.
        /// </summary>
        public Vector2 Anchor2 { get { return mLinearSpring.AttachPoint2; } set { mLinearSpring.AttachPoint2 = value; } }

        /// <summary>
        /// Gets or sets the length the spring will be when it’s not pulling or pushing.
        /// </summary>
        public Single RestLength { get { return mLinearSpring.RestLength; } set { mLinearSpring.RestLength = value; } }

        /// <summary>
        /// Gets or sets the pull/push of the spring. Think of this as the force of the spring.
        /// </summary>
        public Single SpringConstant { get { return mLinearSpring.SpringConstant; } set { mLinearSpring.SpringConstant = value; } }

        /// <summary>
        /// Defines the maximum allowed value that SpringError can reach before the spring is broken.
        /// </summary>
        public Single Breakpoint { get { return mLinearSpring.Breakpoint; } set { mLinearSpring.Breakpoint = value; } }

        /// <summary>
        /// Gets or sets the dampening of the spring. Think of this as a shock absorber.
        /// </summary>
        public Single DampingConstant { get { return mLinearSpring.DampingConstant; } set { mLinearSpring.DampingConstant = value; } }

        /// <summary>
        /// Creates a new instance of a LinearSpring component.
        /// </summary>
        /// <param name="gameObject">The GameObject this Component is attached to.</param>
        public LinearSpring(GameObject gameObject)
            : base(gameObject)
        {
            mLinearSpring = new FarseerGames.FarseerPhysics.Dynamics.Springs.LinearSpring();
            mSpring = mLinearSpring;
        }

        /// <summary>
        /// Create the LinearSpring, call this after setting any initial values such as the ConnectedBody.
        /// </summary>
        public override void Create()
        {
            mLinearSpring = SpringFactory.Instance.CreateLinearSpring(PhysicsManager.Instance.Simulator, GameObject.RigidBody.Body, Anchor1, ConnectedBody.Body, Anchor2, SpringConstant, DampingConstant); 
            mSpring = mLinearSpring;
        }
    }
}
